#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#

#
# Run a backup of the Bacula build directory on two Volumes
#   then copy it to another device.             
#
# This script uses the virtual disk autochanger and migration scripts
#
TestName="copy-volume-test"
JobName=MigVolBackup
. scripts/functions


scripts/cleanup
scripts/copy-migration-confs
scripts/prepare-disk-changer
echo "${cwd}/build" >${cwd}/tmp/file-list
#cp ${cwd}/bin/bacula-sd.conf ${cwd}/tmp/1
#sed "s%# Maximum File Size%  Maximum File Size%" ${cwd}/tmp/1 >${cwd}/bin/bacula-sd.conf

change_jobname NightlySave $JobName
sed 's/migrate/copy/g' ${cwd}/bin/bacula-dir.conf > ${cwd}/tmp/1
sed 's/Migrate/Copy/g' ${cwd}/tmp/1 > ${cwd}/bin/bacula-dir.conf
rm -f ${cwd}/build/po/test.po >/dev/null

start_test

#
# Note, we first backup into Pool Default, 
#          then Copy into Pool Full. 
#              Pool Default uses Storage=File
#              Pool Full    uses Storage=DiskChanger

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
setdebug level=50 storage=DiskChanger
@#setdebug level=100 Dir
label storage=File volume=FileVolume001 Pool=Default
label storage=File volume=FileVolume002 Pool=Scratch
update Volume=FileVolume001 MaxVolBytes=3000000 pool=Default
label storage=DiskChanger volume=ChangerVolume001 slot=1 Pool=Scratch drive=0
label storage=DiskChanger volume=ChangerVolume002 slot=2 Pool=Scratch drive=0
@# 
run job=$JobName yes
wait
run job=$JobName yes
wait
update volume=FileVolume001 VolStatus=Used
update volume=FileVolume002 VolStatus=Used
@#list volumes
@#list jobs
messages
@$out ${cwd}/tmp/log11.out
@############################################################
@# should copy one job, and the second is empty
@############################################################
@# setdebug level=11 dir
@echo "Run copy-volume Job"
@#setdebug level=100 dir
run job=copy-volume yes
@sleep 5 
list jobs
llist jobid=1
llist jobid=2
llist jobid=3
llist jobid=4
llist jobid=5
status storage=DiskChanger
wait
list volumes
list jobs
llist jobid=1
llist jobid=2
llist jobid=3
llist jobid=4
llist jobid=5
sql
select * from JobMedia where JobId=1;

messages
@$out ${cwd}/tmp/log2.out
@############################################################
@# now do a restore
@# Expect: 
@#  Check that copy uses JobId 1,2
@#  Use File instead of DiskChanger
@#  Files are there        
@############################################################
@# Note, here we are restoring from the original backup,
@#  so if you specify storage=DiskChanger the restore will fail
restore where=${cwd}/tmp/bacula-restores select
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File
check_restore_diff

# cleanup previous restore
rm -rf ${cwd}/tmp/bacula-restores

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@############################################################
@# Purge File backup, and restore again
@# Expect:
@#  Check that copy uses other JobId
@#  Use DiskChanger
@#  Files are there
@############################################################
@$out ${cwd}/tmp/log3.out
setdebug level=50 storage=DiskChanger
purge volume=FileVolume001 yes
purge volume=FileVolume002 yes
@# Note, here we are restoring from the original backup,
@#  so if you specify storage=DiskChanger the restore will fail
restore where=${cwd}/tmp/bacula-restores select
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=DiskChanger
check_restore_diff

# cleanup previous restore
rm -rf ${cwd}/tmp/bacula-restores
touch ${cwd}/build/po/test.po

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@############################################################
@# Run a new Incremental, restore melting Copy and Backup
@# and copy the new job to Changer
@# Expect:
@#  Files are there
@#  Copy OK
@############################################################
@$out ${cwd}/tmp/log4.out
run job=$JobName yes
wait
messages
setdebug level=50 storage=DiskChanger
restore where=${cwd}/tmp/bacula-restores select all
done
yes
wait
messages
update volume=FileVolume001 VolStatus=Used
update volume=FileVolume002 VolStatus=Used
run job=copy-volume yes
wait
messages
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=DiskChanger
check_restore_diff

stop_bacula

get_mig_info 1 ${cwd}/tmp/log11.out

if [ -z "$RET" -o "$RET" = 0 ]; then
    bstat=2
    print_debug "The first job must have been copied"
fi

get_mig_info 2 ${cwd}/tmp/log11.out

if [ -n "$RET" ]; then
    bstat=2
    print_debug "The second job has no files, it can't have been copied"
fi

JOBID=`awk -F= '/Job queued. JobId/ {if(!f){print $2; f=1;}}' ${cwd}/tmp/log4.out`
get_mig_info $JOBID ${cwd}/tmp/log4.out

if [ -z "$RET" -o "$RET" = 0 ]; then
    bstat=2
    print_debug "The new incremental job must have been copied"
fi

check_two_logs
end_test
